import {ListView, Tag} from 'antd-mobile';
import React,{Component} from "react";
import axios from "axios"
import {enableTopologicalTravel} from "echarts/src/util/component";
var data_list=[]
const NUM_ROWS = 20;
let pageIndex = 0;

function genData(pIndex = 0) {
    const dataBlob = {};
    for (let i = 0; i < NUM_ROWS; i++) {
        const ii = (pIndex * NUM_ROWS) + i;
        dataBlob[`${ii}`] = `row - ${ii}`;
    }
    return dataBlob;
}
// 将获取的数据中属性为数字的转换为文字
function numberName(newList){
    for(let i=0;i<newList.length;i++) {
        var ratio = newList[i].totalIncomeRadio;
        newList[i].totalIncomeRadio = Number(ratio*100).toFixed(2)+"%";
        switch (newList[i].comBaseCate) {
            case 1:
                newList[i].comBaseCate = "股票型";
                break;
            case 2:
                newList[i].comBaseCate = "基金型";
                break;
            case 3:
                newList[i].comBaseCate = "混合型";
                break;
        }
        switch (newList[i].comBaseRiskLevel) {
            case 1:
                newList[i].comBaseRiskLevel ="谨慎型";
                break;
            case 2:
                newList[i].comBaseRiskLevel ="稳健型";
                break;
            case 3:
                newList[i].comBaseRiskLevel ="平衡型";
                break;
            case 4:
                newList[i].comBaseRiskLevel ="进取型";
                break;
            default:
                newList[i].comBaseRiskLevel ="激进型";
                break;
        }
        switch (newList[i].isUsed) {
            case 0:
                newList[i].isUsed ="已下架";
                break;
            case 1:
                newList[i].isUsed="预售";
                break;
            case 2:
                newList[i].isUsed ="正在运行";
                break;
        }
    }
    return newList;
};
export default class ListViewProductList extends Component {
    constructor(props) {
        super(props);
        const dataSource = new ListView.DataSource({
            rowHasChanged: (row1, row2) => row1 !== row2,
        });
        this.state = {
            dataSource,
            isLoading: true,
        };
    }
    //获取页面数据
   componentWillMount() {
        console.log("122")
        axios({
            method:"GET",
            url:"http://39.101.141.248:9527/combinations/getCombinationsList",
        }).then(res=>{
           console.log(res.data);
           data_list=numberName(res.data.combinationList)
        }).catch(error=>{
            console.log(error);
        });
    }
    //请求初始化数据
    componentDidMount() {
        // you can scroll to the specified position
        // setTimeout(() => this.lv.scrollTo(0, 120), 800);

        // simulate initial Ajax
        setTimeout(() => {
            this.rData = genData();
            this.setState({
                dataSource: this.state.dataSource.cloneWithRows(this.rData),
                isLoading: false,
            });
        }, 600);
    }
    // 分页请求
    onEndReached = (event) => {
        // load new data
        // hasMore: from backend data, indicates whether it is the last page, here is false
        if (this.state.isLoading && !this.state.hasMore) {
            return;
        }
        console.log('reach end', event);
        this.setState({isLoading: true});
        setTimeout(() => {
            this.rData = {...this.rData, ...genData(++pageIndex)};
            this.setState({
                dataSource: this.state.dataSource.cloneWithRows(this.rData),
                isLoading: false,
            });
        }, 1000);
    }

    render() {
        const scroll=false;
        const separator = (sectionID, rowID) => (
            <div
                key={`${sectionID}-${rowID}`}
                style={{
                    backgroundColor: '#F5F5F9',
                    height: 8,
                    borderTop: '1px solid #ECECED',
                    borderBottom: '1px solid #ECECED',
                }}
            />
        );
        let index = data_list.length - 1;
        //列表的item
        const row = (rowData, sectionID, rowID) => {
            if (index < 0) {
                index = data_list.length - 1;
            }
            const obj = data_list[index--];
            return (
                <div key={obj.comBaseId} style={{ padding: '0 15px' }}>
                    <div
                        style={{
                            lineHeight: '50px',
                            color: '#888',
                            fontSize: 18,
                            borderBottom: '1px solid #F6F6F6',
                        }}
                    >
                        {obj.comBaseName}
                        <Tag style={{color:"#f8a83f",marginLeft:20}}>{obj.comBaseCate}</Tag>
                    </div>
                    <div style={{ display: '-webkit-box,flex', padding: '10px 0' }}>
                       {/* <img style={{ height: '64px', marginRight: '15px' }} src={obj.img} alt="" />*/}
                        <div style={{ lineHeight: 1 }}>
                            <div className="productList" >
                                <ul className="productList-ul" >
                                    <li className="font3">
                                        {obj.comBaseRiskLevel}
                                        <p className="gray-text">风险等级</p>
                                    </li>
                                    <li className="font1">
                                        {obj.totalIncomeRadio}
                                        <p className="gray-text">成立以来收益率</p>
                                    </li>
                                    <li className="font2">
                                        {obj.isUsed}
                                        <p className="gray-text">组合状态</p>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            );
        };
        return(
            <div className={scroll ? 'list-scroll' : ''} >
                <ListView
                    ref={el => this.lv = el}
                    dataSource={this.state.dataSource}
                    /*renderHeader={() => <span>header</span>}*/
                    renderFooter={() => (<div style={{ padding: 30, textAlign: 'center' }}>
                        {this.state.isLoading ? 'Loading...' : 'Loaded'}
                    </div>)}
                    renderRow={row}
                    renderSeparator={separator}
                    className="am-list"
                    pageSize={3}
                    scrollEventThrottle={5}
                    useBodyScroll
                    /*renderBodyComponent={() => <MyBody />}*/
                    onScroll={() => { console.log('scroll'); }}
                    scrollRenderAheadDistance={500}
                    onEndReached={this.onEndReached}
                    onEndReachedThreshold={10}
                />
            </div>
        )
    }
}